package com.stop.activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.stop.R;
import com.stop.controller.StopController;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class CriarSalaActivity extends Activity {

	private ProgressDialog dialog;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_criar_sala);
		
		Button concluir = (Button) findViewById(R.id.btnConcluir);
		concluir.setOnClickListener(new View.OnClickListener() {
		    public void onClick(View v) {
		    	
		    	dialog = ProgressDialog.show(CriarSalaActivity.this, "", "Loading. Please wait...", true);
		    	new CriarSala().execute();
		    	
		    }
		});
		
	}
	
	
	private class CriarSala extends AsyncTask<Void, Void, Void> {

		private JSONObject response;

		protected Void doInBackground(Void... params) {
			JSONObject sala = new JSONObject();
	    	
	    	try{
	    		
	    		TextView txtNome =(TextView) findViewById(R.id.txtNomeSala);
	    		sala.put("nome", txtNome.getText());
	    		
		    	RadioGroup vagas = (RadioGroup)findViewById(R.id.radioJogadores);
		    	
		    	switch (vagas.getCheckedRadioButtonId()) {
				case R.id.radioJogadores1:
					sala.put("vagas", 8);
					break;
				case R.id.radioJogadores2:
					sala.put("vagas", 12);
					break;
				case R.id.radioJogadores3:
					sala.put("vagas", 16);
					break;
				default:
					break;
				}
		    	
		    	RadioGroup rodadas = (RadioGroup)findViewById(R.id.radioPartidas);
		    	
		    	switch (rodadas.getCheckedRadioButtonId()) {
				case R.id.radioPartidas6:
					sala.put("rodadas", 6);
					break;
				case R.id.radioPartidas8:
					sala.put("rodadas", 8);
					break;
				case R.id.radioPartidas10:
					sala.put("rodadas", 10);
					break;
				case R.id.radioPartidas12:
					sala.put("rodadas", 12);
					break;
				default:
					break;
				}
		    	
		    	JSONArray temas = new JSONArray();
		    	
		    	CheckBox b1 = ((CheckBox) findViewById(R.id.checkBox1));
		    	if(b1.isChecked()){
		    		temas.put(b1.getText());
		    	}
		    	
		    	CheckBox b2 = ((CheckBox) findViewById(R.id.checkBox2));
		    	if(b2.isChecked()){
		    		temas.put(b2.getText());
		    	}
		    	
		    	CheckBox b3 = ((CheckBox) findViewById(R.id.checkBox3));
		    	if(b3.isChecked()){
		    		temas.put(b3.getText());
		    	}
		    	
		    	CheckBox b4 = ((CheckBox) findViewById(R.id.checkBox4));
		    	if(b4.isChecked()){
		    		temas.put(b4.getText());
		    	}
		    	
		    	CheckBox b5 = ((CheckBox) findViewById(R.id.checkBox5));
		    	if(b1.isChecked()){
		    		temas.put(b5.getText());
		    	}
		    	
		    	CheckBox b6 = ((CheckBox) findViewById(R.id.checkBox6));
		    	if(b6.isChecked()){
		    		temas.put(b6.getText());
		    	}
		    	
		    	CheckBox b7 = ((CheckBox) findViewById(R.id.checkBox7));
		    	if(b7.isChecked()){
		    		temas.put(b7.getText());
		    	}
		    	
		    	CheckBox b8 = ((CheckBox) findViewById(R.id.checkBox8));
		    	if(b8.isChecked()){
		    		temas.put(b8.getText());
		    	}
		    	
		    	CheckBox b9 = ((CheckBox) findViewById(R.id.checkBox9));
		    	if(b9.isChecked()){
		    		temas.put(b9.getText());
		    	}
		    	
		    	CheckBox b10 = ((CheckBox) findViewById(R.id.checkBox10));
		    	if(b10.isChecked()){
		    		temas.put(b10.getText());
		    	}
		    	
		    	CheckBox b11 = ((CheckBox) findViewById(R.id.checkBox11));
		    	if(b11.isChecked()){
		    		temas.put(b11.getText());
		    	}
		    	
		    	CheckBox b12 = ((CheckBox) findViewById(R.id.checkBox12));
		    	if(b12.isChecked()){
		    		temas.put(b12.getText());
		    	}
		    	
		    	CheckBox b13 = ((CheckBox) findViewById(R.id.checkBox13));
		    	if(b13.isChecked()){
		    		temas.put(b13.getText());
		    	}
		    	
		    	CheckBox b14 = ((CheckBox) findViewById(R.id.checkBox14));
		    	if(b14.isChecked()){
		    		temas.put(b14.getText());
		    	}
		    	
		    	sala.put("temas", temas);
		    	
		    	response = StopController.getInstancia().criarSala(sala);
		    	
	    	}catch(JSONException e){
	    		e.printStackTrace();
	    	}catch(Exception e){
	    		e.printStackTrace();
	    	}
			return null;
		}

		protected void onPostExecute(Void result) {

			dialog.cancel();
			
			try {
				boolean returnError = response.getBoolean("error");
				
				if(returnError){
					Toast.makeText(getApplicationContext(), response.getString("errorMsg"), 10000).show();
				}else{
					
				}
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
		}

	}


}
